//translate.js

var util = require('../../utils/util.js')
var app = getApp()
const domainUrl = app.globalData.domainUrl
// const domainUrl = "http://localhost:8771/"

Page({
  data: {
    duration: 2000,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    loading: false,
    plain: false,
    userInfo: {},
    currentID: '0',
    menuItems: [
      {
        courseName: '卡片兑换',
        id: '0'
      },
      {
        courseName: '集卡册兑换',
        id: '1'
      },
      {
        courseName: '卡片分解',
        id: '2'
      }
    ]
  },
  onLoad: function (params) {
    util.login(this.route);
    var that = this
    var openid = wx.getStorageSync('my_openid');
    wx.request({
      url: domainUrl + 'transList?openId=' + openid,
      success: function (res) {
        console.log(res.data);
        that.setData({
          dataList: res.data.data
        })
      }
    })
  },
  tapMenuItem: function (e) {
    var that = this;
    console.log(e.currentTarget.id);
    console.log(e.currentTarget);
    var id = e.currentTarget.id;
    var openid = wx.getStorageSync('my_openid');
    var url;
    if (id == '0') {
      url = domainUrl + 'transList?openId='+openid;
    } else if(id == '1') {
      url = domainUrl + 'collectBookList?openId='+openid;
    } else {
      url = domainUrl + 'decomposeList?openId=' + openid;
    }
    wx.request({
      url: url,
      success: function (res) {
        console.log(res.data);
        that.setData({
          dataList: res.data.data,
          currentID: e.currentTarget.id
        })
      }
    })
  },
  onReady: function () {
    wx.setNavigationBarTitle({ title: '兑换列表', success: function (res) { } });
    this.innerAudioContext = wx.createInnerAudioContext();
  },
  onShow: function () {
    console.log("显示图片")
    var that = this;
    var id = this.data.currentID;
    var openid = wx.getStorageSync('my_openid');
    var url;
    if (id == '0') {
      url = domainUrl + 'transList?openId=' + openid;
    } else if (id == '1') {
      url = domainUrl + 'collectBookList?openId=' + openid;
    } else {
      url = domainUrl + 'decomposeList?openId=' + openid;
    }
    wx.request({
      url: url,
      success: function (res) {
        console.log(res.data);
        that.setData({
          dataList: res.data.data,
          currentID: id
        })
      }
    })

  },
  duihuan: function (event) {
    console.log(event);
    var that = this;
    var openid = wx.getStorageSync('my_openid');
    var count = event.currentTarget.dataset.item.count;
    var msg = event.currentTarget.dataset.item.msg;
    var fr = event.currentTarget.dataset.item.from;
    var to = event.currentTarget.dataset.item.to;
    var mtd = event.currentTarget.dataset.item.mtd;
    console.log(count);
    if (count == "0") {
      console.log(count);
      wx.showToast({
        title: '数量不足',
        icon: 'success',
        duration: 2000,
        mask: false
      })
      return;
    }
    wx.showModal({
      title: '提示',
      content: msg,
      success: function (sm) {
        if (sm.confirm) {
          var mtdParam = {
            mtd: mtd,
            openid: openid,
            fr: fr,
            to: to
          }
          that.duihuanMtd(mtdParam);
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },
  duihuanMtd: function (event) {
    var mtd = event.mtd;
    var openid = event.openid;
    var that = this;
    var fr = event.fr;
    var to = event.to;
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    wx.request({
      url: domainUrl + mtd + '?openId=' + openid + "&from=" + fr + "&to=" + to,
      success: function (res) {
        console.log(res.data.msg);
        wx.hideLoading();
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
        that.onShow();
      }
    })
  },
  hideRule: function () {
    this.setData({
      isRuleTrue: false
    })
  },
  showRule: function () {
    var that = this;
    wx.request({
      url: domainUrl + 'duihuanHelp',
      success: function (res) {
        console.log(res.data);
        that.setData({
          isRuleTrue: true,
          rule: res.data
        })
      }
    })
  },
  translateText: function (e) { this.setData({ translateValue: e.detail.value }) },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '水浒卡',
      path: '/pages/card/card',
      success: function (res) { },
      fail: function (res) { }
    }
  }
})